import { fetchAppleList } from "../../api/list";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchVal: "",
    PageIndex: 0,
    PageSize: 10,
    hasMore: false,
    noData: false,
    loading: false,
    persons: [],
    personNum: 0,
    personQty: 0,
    yearOptions: [
      { text: "2023.10.1-2024.9.30", value: 2024 },
      { text: "新款商品", value: 1 },
      { text: "活动商品", value: 2 },
    ],
    year: 2024,
    showTypes: false,
    typeActions: [
      {
        name: "冷风库",
      },
      {
        name: "气调库",
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  /**
   * 获取果农入库列表
   */
  getAppleList() {
    let params = {
      year: this.data.year,
      kutype: 0,
      PageSize: this.data.PageSize,
      PageIndex: this.data.PageIndex,
      Search: this.data.searchVal,
    };
    fetchAppleList(params)
      .then((res) => {
        this.setData({
          loading: true,
        });
        if (res.code === 200) {
          //接口返回无结果了
          //接口返回有数据
          let _arr = [...this.data.persons, ...res.data.persons];
          // console.log("arr?>>", _arr);
          this.setData({
            personNum: res.data.personNum,
            personQty: res.data.personQty,
            persons: _arr,
          });
          // console.log("请求结果", res.data.persons.length);
          if (
            res.data.persons.length === 0 ||
            res.data.persons.length < this.data.PageSize
          ) {
            setTimeout(() => {
              this.setData({
                noData: true,
              });
            }, 1000);
          }

          setTimeout(() => {
            this.setData({
              loading: false,
            });
          }, 1000);
          wx.stopPullDownRefresh();
        }
      })
      .catch((err) => {
        wx.showToast({
          title: "请求错误:" + err,
        });
        setTimeout(() => {
          wx.navigateTo({
            url: "/pages/login/login",
          });
        }, 600);
      });
  },

  onSearchClick() {
    this.setData({
      PageIndex: 0,
      persons: [],
    });
    this.getAppleList();
  },

  goDetail(e) {
    wx.navigateTo({
      url: "/pages/detail/detail?personId=" + e.currentTarget.dataset.id,
    });
  },

  add() {
    this.setData({
      showTypes: true,
    });
  },
  onClose() {
    this.setData({ showTypes: false });
  },
  onTypeSelect(event) {
    console.log(event.detail);
    if (event.detail.name === "冷风库") {
      wx.navigateTo({
        url: "/pages/add/add?op=add&type=cold",
      });
    } else {
      wx.navigateTo({
        url: "/pages/add/add?op=add&type=qitiao",
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      PageIndex: 0,
      noData: false,
      persons: [],
    });
    this.getAppleList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    let _token = wx.getStorageSync("token");
    if (_token) {
      this.setData({
        persons: [],
        PageIndex: 0,
        noData: false,
      });
      this.getAppleList();
    } else {
      wx.navigateTo({
        url: "/pages/login/login",
      });
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.noData) return;
    this.setData({
      PageIndex: this.data.PageIndex + 1,
    });
    this.getAppleList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
